package com.example.rg.generator.Filter;

import com.alibaba.fastjson.JSON;
import com.example.rg.generator.commons.BaseContext;
import com.example.rg.generator.commons.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    //    路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取请求处理的URL
        String requestURI = request.getRequestURI();
        //获取不需要处理的URL
        String[] urls = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/login",
                "/user/sendMsg",
                "/common/**"
        };
        boolean check = check(urls, requestURI);
        log.info("询问询问{}询问询问", requestURI);
        if (check) {
            log.info("本次工作不需要处理");
            filterChain.doFilter(request, response);
            return;
        }
        // 判断是否为登陆状态
        if (request.getSession().getAttribute("employee") != null) {

            Object employee = request.getSession().getAttribute("employee");
            log.info("用户已登陆id为{}", employee);

            BaseContext.setCurrentId((Long) employee);

            filterChain.doFilter(request, response);
            return;
        }
        if (request.getSession().getAttribute("user") != null) {
            Object user = request.getSession().getAttribute("user");

            BaseContext.setCurrentId((Long) user);

            log.info("用户已登陆");
            //多线程获取id
            filterChain.doFilter(request, response);
            return;
        }
        log.info("用户未登录");
        //传输字节流到前端
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }

    public boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            if (PATH_MATCHER.match(url, requestURI)) {
                return true;
            }
        }
        return false;
    }
}
